package com.ruoyi.consumer.aop.convert;

import com.ruoyi.consumer.domain.*;
import com.ruoyi.consumer.service.impl.*;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CorporateOperateStrategy implements ConvertStrategy {

    @Autowired
    private CUserServiceImpl cUserService;

    @Autowired
    private CompanyBindServiceImpl companyBindService;

    @Autowired
    private CDeviceOrdersServiceImpl cDeviceOrdersService;

    @Autowired
    private CCarServiceImpl cCarService;

    @Autowired
    private CFillingFacilitServiceImpl cFillingFacilitService;

    @Autowired
    private CStatisticTableServiceImpl cStatisticTableService;

    @Override
    public Object takeOrderOperate(Integer facilitId, Integer userId, Long amount, String snNumber, Long orderId) {
        System.out.println("公司下单切面:" + facilitId + " userId:" + userId + " amount:" + amount + " snNumber:" + snNumber + " orderId:" + orderId);
        CDeviceOrders cDeviceOrders = new CDeviceOrders();
        cDeviceOrders.setFillerId(String.valueOf(orderId));
        List<CDeviceOrders> realOrders = cDeviceOrdersService.selectCDeviceOrdersList(cDeviceOrders);
        CUser cUser = cUserService.selectCUserByUserId(userId);
        CFillingFacilit cFillingFacilit = cFillingFacilitService.selectCFillingFacilitByFacilitId(facilitId);
        Assert.notNull(cUser, "用户信息不存在");
        Assert.notNull(cFillingFacilit, "设备信息不存在");
        if (realOrders.size() == 0) {
            System.out.println("实际订单orderId不存在");
            return takeOrderOperate(facilitId, userId, amount, snNumber, orderId);
        }
        //获取公司绑定信息
        CompanyBind companyBind = new CompanyBind();
        if (cUser.getUserRole() == 0) {
            companyBind.setConsumerId(userId);
        } else if (cUser.getUserRole() == 1) {
            companyBind.setSysUserId(userId.longValue());
        } else if (cUser.getUserRole() == 2) {
            companyBind.setMangerId(userId);
        }

        List<CompanyBind> companyBinds = companyBindService.selectCompanyBindList(companyBind);

        //获取机器订单
        CDeviceOrders realOrder = realOrders.get(0);
        //获取车辆id
        CCar cCar = cCarService.selectCCarByCarId(cUser.getCarId());
        String plateNum = null;
        Integer carId = null;
        if (cCar != null) {
            plateNum = cCar.getPlateNum();
            carId = cCar.getCarId();
        }
        String userName = null;
        if (cUser.getUserName() != null && !cUser.getUserName().equals("")) {
            userName = cUser.getUserName();
        } else {
            userName = cUser.getUserPhone();
        }

        //赋值给统计表
        CStatisticTable cStatisticTable = new CStatisticTable();
        cStatisticTable.setSn(snNumber);
        cStatisticTable.setFacilitId(facilitId);
        cStatisticTable.setUserId(userId);
//        cStatisticTable.setFuserId();
        cStatisticTable.setUserName(userName);
        cStatisticTable.setFillerId(orderId.toString());
        cStatisticTable.setOrderPrice(realOrder.getOrderPrice());
        //获取绑定关系
        if (companyBinds.size() != 0) {
            CompanyBind companyBind1 = companyBinds.get(0);
            cStatisticTable.setCompanyId(companyBind1.getCompanyId());
            cStatisticTable.setMangerId(companyBind1.getMangerId());
            cStatisticTable.setLeaderId(Math.toIntExact(companyBind1.getSysUserId()));
        }
        cStatisticTable.setCarId(carId);
        cStatisticTable.setPlateNum(plateNum);
        cStatisticTable.setStatisticType(1);
        cStatisticTable.setSn(realOrder.getSn());
        cStatisticTable.setFacilitProvince(cFillingFacilit.getFacilitProvince());
        cStatisticTable.setFacilitCity(cFillingFacilit.getFacilitCity());
        cStatisticTable.setFacilitArea(cFillingFacilit.getFacilitArea());
        cStatisticTable.setFacilitLat(cFillingFacilit.getFacilitLat());
        cStatisticTable.setFacilitLng(cFillingFacilit.getFacilitLng());
        cStatisticTable.setOrderFilled(0L);
        cStatisticTable.setOrderMoney(0);
        cStatisticTable.setOrderPrice(0);

        int i = cStatisticTableService.insertCStatisticTable(cStatisticTable);
        if (i > 0) {
            System.out.println("公司下单统计成功" + orderId);
            return orderId;
        }

        return orderId;
    }

    @Override
    public Object getMinOrderOperate(Long orderId) {
        System.out.println("公司小订单切面" + orderId);
        CStatisticTable cStatisticTable = new CStatisticTable();
        cStatisticTable.setFillerId(orderId.toString());
        List<CStatisticTable> cStatisticTables = cStatisticTableService.selectCStatisticTableList(cStatisticTable);
        CDeviceOrders cDeviceOrders = new CDeviceOrders();
        cDeviceOrders.setFillerId(orderId.toString());
        List<CDeviceOrders> realOrders = cDeviceOrdersService.selectCDeviceOrdersList(cDeviceOrders);
        if (realOrders.isEmpty()) {
            Assert.notNull(realOrders, "实际订单不存在");
        }
        if (cStatisticTables.isEmpty()) {
            Assert.notNull(cStatisticTables, "统计表不存在");
        }
        CStatisticTable statisticTable = cStatisticTables.get(0);
        statisticTable.setOrderFilled(realOrders.get(0).getOrderFilled());
        statisticTable.setOrderMoney(realOrders.get(0).getOrderFilledMoney());
        statisticTable.setOrderPrice(realOrders.get(0).getOrderPrice());
        int i = cStatisticTableService.updateCStatisticTable(statisticTable);
        if (i > 0) {
            System.out.println("公司小订单统计成功");
            return null;
        }

        return null;
    }

    @Override
    public Object getWxOrderOperate(String weixinOrderId) {
        return null;
    }

    @Override
    public Object getWxMinOrderOperate(Long orderId, String out_trade_no) {
        return null;
    }
}
